﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using RobotCtrl;

namespace RobotView
{
    public partial class LedView : UserControl
    {
        private bool state;
        private Led led;

        public LedView()
        {
            InitializeComponent();
            state = false;
        }

        public bool State
        {
            get { return state; }
            set
            {
                state = value;
                if (state)
                {
                    pictureBox1.Image = RobotView.Properties.Resource.LedOn;
                }
                else
                {
                    pictureBox1.Image = RobotView.Properties.Resource.LedOff;
                }
            }
        }

        public Led Led
        {
            get
            {
                return led;
            }

            set
            {
                led = value;
                if (led != null) led.LedStateChanged -= new EventHandler<LedEventArgs>(LedStateChanged);
                led.LedStateChanged += new EventHandler<LedEventArgs>(LedStateChanged);
                if (led != null) led.LedStateChanged += new EventHandler<LedEventArgs>(LedStateChanged);
            }
        }


        /// <summary>
        /// Abfrage, ob synchronisiert werden muss, wenn Aufruf durch anderen Thread.
        /// -> Eintrag in Message Queue
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void LedStateChanged(object sender, LedEventArgs e)
        {
            if (InvokeRequired)
            {
                // Zugriff von anderem Thread => Synchronisieren
                Invoke(new EventHandler<LedEventArgs>(LedStateChanged), sender, e);
            }
            else
            {
                // Zugriff von GUI-Thread
                State = e.LedEnabled;
            }            
        }
    }
}


